<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <h3>混入对象</h3>
      <p>{{ count }}</p>
      <child></child>
    </div>

    <script src="../Vue.js"></script>
    <script>
      // 混入：分发可复用功能
      // 将相同的选项提出来

      // 创建混入对象
      // 语法和创建局部组件完全相同，除了没有template选项

      // 混入对象的内容会和组件的选项做合并, 如果重名，组件的优先级更高
      // 生命周期也可以混入, 相同的生命周期都会执行

      const myMixin = {
        data() {
          return {
            count: 10,
            msg: "456",
          };
        },
        methods: {
          fn() {
            console.log(123);
          },
        },
        mounted() {
          console.log("myMixin mounted");
        },
      };

      const Child = {
        mixins: [myMixin],
        template: `
          <div>
            child - {{ count }}
          </div>
        `,
        mounted() {
          this.fn();
        },
      };

      new Vue({
        el: "#app",
        components: {
          Child,
        },
        mixins: [myMixin],
        data() {
          return {
            msg: "123",
          };
        },
      });
    </script>
  </body>
</html>
